package controllers;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;
import com.sun.org.apache.bcel.internal.generic.FSUB;



import models.Location;
import models.RatingNom;
import models.SolutionAdoption;
import models.SolutionSubmission;




public class ajxAdoptionSolutionConnectServlet {
	
	
	@SuppressWarnings("unused")
	private boolean ChectachievedType(String _Value)
	{
		boolean _tmp =false;
		try
		{
			if (_Value.equals("Safety") ||_Value.equals("Customer satisfaction") ||_Value.equals("Employee engagement ")  ){
				_tmp =true;
			}
			return _tmp;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return _tmp;

	}
	
	private int CheckRating(String _Value)
	{
		int _tmp =0;
		try
		{
			_Value =_Value.trim();
			 if (_Value.equals("Very easy to implement")){
				 _tmp =3;
			 }
			 else if (_Value.equals("Somewhat easy to implement"))
			 {
				 _tmp =2;
			 }
			 else if (_Value.equals("Difficult to implement"))
			 {
				 _tmp =1;
			 }
			 return _tmp;
		}
		catch (Exception e) {
			// TODO: handle exception
		}
		return _tmp;
	}
	
	public boolean isFloat( String input )  
	{  
	   try  
	   {  
	      Float.parseFloat( input );  
	      return true;  
	   }  
	   catch (Exception e) {
		   return false;
		// TODO: handle exception
	}
	   
	}
	
	
	public List<HashMap<String,Object>> getAllAdoption(String _value) {
		List<HashMap<String, Object>> objList = new LinkedList<HashMap<String,Object>>();
    	try {
    		Objectify ofy = ObjectifyService.begin();
    		boolean FiterByBenefit =false;
    	    if (!(_value.equals("--Select Primary Benefit Category--") || _value.equals("")) ){
    	    	FiterByBenefit =true;
    	    }
 
    	    
			Objectify ofySolution = ObjectifyService.begin();
			
			Query<SolutionSubmission> DsSol ;
			if (FiterByBenefit){
				DsSol = ofySolution.query(SolutionSubmission.class).filter("achievedTypeDesc", _value);
			}
			else
			{
				DsSol = ofySolution.query(SolutionSubmission.class);
			}
			
			for ( SolutionSubmission fSu : DsSol.fetch() ) 
			{
				int easyRankDesc =0;
				int CountAp =0;
				int impDays=0;
				String OriginalBenefit="";
			    Query<SolutionAdoption> q ;

	    	    q = ofy.query(SolutionAdoption.class).filter("solID",fSu.getSolID());
	 
	    	    CountAp = q.countAll();
	    	    float TotalOriginalBenefit =0;
	    	    if (CountAp>0){
	    	        boolean Flg=false;
	    	    	for (SolutionAdoption f : q.fetch() )
	    	    	{
	    	    		if (!Flg) {
	    	    			OriginalBenefit = f.getAchievedUnits();
	    	    			Flg =true;
	    	    		}
		    			if (isFloat(f.getAchievedValue()))
    	    			{
    	    				TotalOriginalBenefit +=Float.parseFloat(f.getAchievedValue());
    	    			}
    	    			
	    	   	    	 easyRankDesc  += CheckRating(f.getEasyRankDesc());
		    			 impDays += f.getImpDays();
	    			   if (!OriginalBenefit.equals( f.getAchievedUnits())){
	    				   OriginalBenefit ="";
	    			   }
	    	    	}
	    	    	
	    	    	
	    	    	HashMap<String, Object> itemMap = new HashMap<String, Object>();
	    	    	itemMap.put("SolCode",  fSu.getSolCode().toString());
					itemMap.put("SolID", fSu.getSolID());
					itemMap.put("OfAdop", CountAp);
					
					if(ChectachievedType(fSu.getAchievedTypeDesc())){
						itemMap.put("Original", "N/A" );
					}
					else{
						itemMap.put("Original", fSu.getAchievedValue() +" "+ fSu.getAchievedUnits() );
					}
					
					if (!OriginalBenefit.isEmpty()){
					     itemMap.put("New", TotalOriginalBenefit +" "+ OriginalBenefit);
					}
					else
					{
						 itemMap.put("New", OriginalBenefit);
					}
					
					itemMap.put("TotalAmount", fSu.getAnnProdSavings()+ " ($USD)");
					itemMap.put("easyRankDesc", easyRankDesc / CountAp);
					itemMap.put("Totaltime", impDays);
					objList.add(itemMap);
	                
	    	    }
			}
			
    	    /*
    	    
    	    // Case 1 hasHardSavings =========true ==========================================================================================
    	    Query<SolutionAdoption> q ;
    	    if(!FiterByBenefit){
    	        q = ofy.query(SolutionAdoption.class).order("solID");
    	    }
    	    else
    	    {
    	        q = ofy.query(SolutionAdoption.class).filter("achievedTypeDesc", _value.trim()).order("solID");
    	    }
    	
    	    
    	    String Original ="";
    	    String OriginalBenefit ="";
    	    int impDays =0;
    	    int count =0;
    	    int TotalOfAdop =0;
    	    boolean Flg=false;
    	    int RowOfQuery = q.countAll();
    	    int LoopCount =0;
    	    int easyRankDesc =0;
    	    
    	    
    	    
    		for (SolutionAdoption f : q.fetch() )
    		{
    			if (count ==0){
    				Original = f.getSolID().toString();
    				OriginalBenefit = f.getAchievedTypeDesc();
    				count =1;
    			}

    			if (Original.equals(f.getSolID().toString()))
    			{
    			   TotalOfAdop +=1;
    			   easyRankDesc  += CheckRating(f.getEasyRankDesc());
    			   impDays += f.getImpDays();
    			   Flg =false;
    			   if (!OriginalBenefit.equals( f.getAchievedTypeDesc())){
    				   OriginalBenefit ="";
    			   }
    			}
    			else
    			{
    				Flg =true;
    			}
    			//if ((RowOfQuery-1) ==LoopCount)Flg =true;
    		
    			if (Flg)
    			{
    				
    				
					Objectify ofySolution = ObjectifyService.begin();
					Query<SolutionSubmission> qSu = ofySolution.query(
							SolutionSubmission.class).filter("solID",
							Long.parseLong(Original));
					for (SolutionSubmission fSu : qSu.fetch()) {
						HashMap<String, Object> itemMap = new HashMap<String, Object>();
						itemMap.put("SolID", Original);
						itemMap.put("OfAdop", TotalOfAdop);
						
						if(ChectachievedType(fSu.getAchievedTypeDesc())){
							itemMap.put("Original", "N/A" );
						}
						else{
							itemMap.put("Original", fSu.getAchievedValue() +" "+ fSu.getAchievedUnits() );
						}
						
						
						itemMap.put("New", OriginalBenefit);
						itemMap.put("TotalAmount", fSu.getAnnProdSavings()
						+ " ($USD)");
						itemMap.put("easyRankDesc", easyRankDesc / TotalOfAdop);
						itemMap.put("Totaltime", impDays);
						objList.add(itemMap);
						
						//Clear Value =================================
							TotalOfAdop = 0;
							OriginalBenefit = "";
							impDays = 0;
						//=============================================
						
						
						//Keep Value New SolID===============================
						   Original = f.getSolID().toString();
						   TotalOfAdop +=1;
						   easyRankDesc  += CheckRating(f.getEasyRankDesc());
						   impDays += f.getImpDays();
						   Flg =false;
						   if (!OriginalBenefit.equals( f.getAchievedTypeDesc())){
							   OriginalBenefit ="";
						   }
					   //==================================================================	
						
					}
					
					
    				   
    				   if (((RowOfQuery-1) ==LoopCount) && !Original.equals(f.getSolID().toString()))
    				   {   TotalOfAdop+=1;
    					   easyRankDesc  = CheckRating(f.getEasyRankDesc());
    	    				ofySolution = ObjectifyService.begin();
    	    				Query<SolutionSubmission> qSu1 = ofySolution.query(SolutionSubmission.class).filter("solID",  f.getSolID());
    	    				   for ( SolutionSubmission fSu : qSu1.fetch() ) 
    	    				    {
    								HashMap<String,Object> itemMap = new HashMap<String,Object>();
    								itemMap.put("SolCode",  f.getSolCode().toString());
    								itemMap.put("SolID",  f.getSolID().toString());
    								itemMap.put("OfAdop",  TotalOfAdop);
    								if(ChectachievedType(fSu.getAchievedTypeDesc())){
    									itemMap.put("Original", "N/A" );
    								}else{
    									itemMap.put("Original", fSu.getAchievedValue() +" "+ fSu.getAchievedUnits() );
    								}
    								itemMap.put("New", OriginalBenefit);
    								itemMap.put("TotalAmount",  fSu.getAnnProdSavings() +" ($USD)");
    								itemMap.put("easyRankDesc",  easyRankDesc/TotalOfAdop);
    								itemMap.put("Totaltime",  impDays);
    								objList.add(itemMap);
    								
    								TotalOfAdop =0;
    								OriginalBenefit="";
    								impDays=0;
    	    				}
    				   }
    			}
    			
    			LoopCount +=1;
    		}
    		*/
    	} catch ( Exception e ) { 
    		e.printStackTrace(); 
    	}
    	return objList;	
	}
	
}
